import json
import os
import numpy as np
from matplotlib import pyplot as plt
from input import input_data
from pre.smoothing import savitzky_golay_filter
from pre.normalization import unit_vector_normalization
from pre.baseline_correction import polynomial_baseline_correction
from pre.data_cleansing import replace_outliers_with_next
from analysis.partial_least_squares import PLS_usage_package
from analysis.MCR_alternating_least_squares import mcr_als

def pre_single_display(file_path, pre_fun):
	cross_platform_path = os.path.normpath(file_path).replace('\\', '/')
	print(cross_platform_path)
	# print(pre_fun)
	if(file_path!=None):
		fig = plt.figure(dpi=300,figsize=(24,8))
		data,_ = input_data(laser_point=1000,file_paths=cross_platform_path)
		plt.plot(data[0])
		while pre_fun:
			cur_fun = pre_fun.pop(0)
			if cur_fun == "SG滤波":
				data = savitzky_golay_filter(data)
			if cur_fun == "归一化":
				data = unit_vector_normalization(data)
			if cur_fun == "基线校正":
				data = polynomial_baseline_correction(data)
			if cur_fun == "异常值剔除":
				data = replace_outliers_with_next(data)
		plt.plot(data[0])
		return fig
	else:
		print("未输入文件路径")


def format_concentration_data(concentration_matrix):
	# 创建一个字典来存储所有的浓度数据
	all_concentrations = {}
	for i, concentration in enumerate(concentration_matrix):
		# 这儿除以100是为了显示成百分比
		concentration_value = float(concentration) / 100
		# 使用浓度位置作为键
		all_concentrations[f"第{i + 1}个浓度"] = concentration_value
	return all_concentrations

def pls(train_floder_input,inference_input,pre_PLS_fun,pls_n_components_input):
	# print(inference_input)
	# print(pre_PLS_fun)
	# print(pls_n_components_input)
	pre_PLS_fun_copy = pre_PLS_fun
	pls = PLS_usage_package(n_components=pls_n_components_input)
	matrix,percentages = input_data(train_floder_input)
	percentages = np.array(percentages)
	while pre_PLS_fun:
		cur_fun = pre_PLS_fun.pop(0)
		if cur_fun == "SG滤波":
			matrix = savitzky_golay_filter(matrix)
		if cur_fun == "归一化":
			matrix = unit_vector_normalization(matrix)
		if cur_fun == "基线校正":
			matrix = polynomial_baseline_correction(matrix)
		if cur_fun == "异常值剔除":
			matrix = replace_outliers_with_next(matrix)
	pls.input_data(matrix, percentages)
	pls.create_PLS_model()
	inference_data, _ = input_data(file_paths=inference_input)
	while pre_PLS_fun_copy:
		cur_fun = pre_PLS_fun_copy.pop(0)
		if cur_fun == "SG滤波":
			inference_data = savitzky_golay_filter(inference_data)
		if cur_fun == "归一化":
			inference_data = unit_vector_normalization(inference_data)
		if cur_fun == "基线校正":
			inference_data = polynomial_baseline_correction(inference_data)
		if cur_fun == "异常值剔除":
			inference_data = replace_outliers_with_next(inference_data)
	concentration_data = pls.inference(inference_data)
	#print(f"concentration_data:{concentration_data}")
	concentration_result = format_concentration_data(concentration_data)
	return concentration_result

def mcrals(MCRALS_floder_input,k_components_input,pre_MCRALS_fun):
	data,_ = input_data(MCRALS_floder_input)
	while pre_MCRALS_fun:
		cur_fun = pre_MCRALS_fun.pop(0)
		if cur_fun == "SG滤波":
			data = savitzky_golay_filter(data)
		if cur_fun == "归一化":
			data = unit_vector_normalization(data)
		if cur_fun == "基线校正":
			data = polynomial_baseline_correction(data)
		if cur_fun == "异常值剔除":
			data = replace_outliers_with_next(data)
	s,c = mcr_als(k_components_input,data)
	print(s.shape)
	mcrals_fig = plt.figure()
	size = s.shape[0]
	for i in range(size):
		plt.plot(s[i-1])
	return mcrals_fig,c

